package com.score.system.controller;

import com.score.system.entity.Score;
import com.score.system.entity.User;
import com.score.system.service.ScoreService;
import com.score.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/find")
public class SearchController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private UserService userService;

    @RequestMapping("/search")
    @ResponseBody
    public ModelAndView findLogs(Model model, String keyword, String selectedValue, HttpSession session) {
        ModelAndView modelAndView  = null;
        List<Score> scores;
        Map<Score, User> map = new LinkedHashMap<>();
        //姓名,年级和分数
        if (selectedValue.equals("姓名")){
            scores = scoreService.getByname(keyword);
            if (scores == null){
                modelAndView = new ModelAndView("/index");
                modelAndView.addObject("message","该姓名不存在");

            }else{
                Collections.sort(scores);
                for (Score score : scores){
                    User selectedUser = userService.findUserById(score.getSid());
                    map.put(score,selectedUser);
                }
                session.setAttribute("scoreList", map);
                modelAndView = new ModelAndView(new RedirectView("/selectDetails"));
            }

        }else if (selectedValue.equals("1")){ //年级
            scores = scoreService.getBygrade(keyword);
            if (scores == null){
                modelAndView = new ModelAndView("/index");
                modelAndView.addObject("message","该年级不存在");

            }else{
                Collections.sort(scores);
                System.out.println(scores);
                for (Score score : scores){
                    User selectedUser = userService.findUserById(score.getSid());
                    map.put(score,selectedUser);
                }
                session.setAttribute("scoreList", map);
                modelAndView = new ModelAndView(new RedirectView("/selectDetails"));
            }

        }else if (selectedValue.equals("2")){ //分数
            int sc = Integer.parseInt(keyword);
            scores = scoreService.getByscore(sc);
            if (scores == null){
                modelAndView = new ModelAndView("/index");
                modelAndView.addObject("message","该分数不存在");

            }else{
                Collections.sort(scores);
                for (Score score : scores){
                    User selectedUser = userService.findUserById(score.getSid());
                    map.put(score,selectedUser);
                }
                session.setAttribute("scoreList", map);
                modelAndView = new ModelAndView(new RedirectView("/selectDetails"));
            }

        }else{
            //全部错误信息
            modelAndView = new ModelAndView("/index");
            modelAndView.addObject("message","输入不合法");
        }

        return modelAndView;


    }
}
